//
//  ST_MyFansCell.swift
//  btvideo
//
//  Created by mac on 2020/5/25.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwifterSwift
import SnapKit




    



class ST_MyFansCell: UICollectionViewCell {
    
    static let width:CGFloat = (ScreenWidth - 20 - 10)/3.0
    
    static let height:CGFloat = ST_MyFansCell.width+49
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .white
        
        
        
        contentView.addSubview(cover_image_view)
        contentView.addSubview(icon_supplement_image_view)
        icon_supplement_image_view.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(icon_supplement_image_view.snp.width)
        }
        
        cover_image_view.snp.makeConstraints { (make) in
            make.center.equalTo(icon_supplement_image_view)
            make.width.height.equalTo(icon_supplement_image_view).dividedBy(115.0/85.0)
//
        }
        
        contentView.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.right.centerX.equalToSuperview()
            make.top.equalTo(icon_supplement_image_view.snp.bottom)
            make.height.equalTo(22)
        }
        
        /*
        contentView.addSubview(sweet_value_label)
        sweet_value_label.snp.makeConstraints { (make) in
            make.left.right.centerX.equalToSuperview()
            make.top.equalTo(content_label.snp.bottom).offset(5)
            make.height.equalTo(17)
        }
        */
        
        contentView.addSubview(follow_btn)
        follow_btn.snp.makeConstraints { (make) in
            make.height.equalTo(21)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate lazy var cover_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = (ST_MyFansCell.width - 30)/2.0
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(push_user_center_handle))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var icon_supplement_image_view:UIImageView = {
        let control = UIImageView()
       return control
    }()
    
    fileprivate lazy var content_label:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        
        control.setTitleColorForAllStates(Color.init(hexString: "#333333")!)
        
//        semanticContentAttribute
        
        control.semanticContentAttribute = .forceRightToLeft
        
        control.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        
        return control
    }()
    
    fileprivate lazy var sweet_value_label:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        
        control.setTitleColorForAllStates(Color.init(hexString: "#333333")!)
        
        control.setImageForAllStates(ST_ImageUtils.imageName("sweet_value_icon")!)
        
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        
        return control
    }()
    
//    fileprivate lazy var content_label:UILabel = {
//        let control = UILabel.init()
//        control.text = ""
//        control.textColor = Color.init(hexString: "#333333")
////        control.numberOfLines = 0
//        control.textAlignment = .center
//        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
//        return control
//    }()
    
    
    fileprivate lazy var follow_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.cornerRadius = 21/2.0
        control.setTitle("喜欢", for: UIControl.State.normal)
        control.setTitle("已喜欢", for: UIControl.State.selected)
        control.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0x42CEDA)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.normal)
        control.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xDFE3E9)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.selected)
        control.setTitleColorForAllStates(UIColor.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        control.addTarget(self, action: #selector(follow_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
//    MARK: - Bind Model
    weak var child_model:ST_MyFansCellModel?{
        didSet{
            
            guard let model = child_model else { return  }
            
            cover_image_view.pin_setImage(from: model.avatar.compress_small_image(width: 400).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            icon_supplement_image_view.pin_setImage(from: AVATAR_SUPPLEMENT(guardTime: model.guardTime, guardContent: model.guardContent)?.url)
            
//            let value = model.intimacy.st_number_description()
//
//            sweet_value_label.setTitleForAllStates(value)
//
//            sweet_value_label.isHidden = model.intimacy < 1
            
            content_label.setTitleForAllStates(model.nickname)
            
            if CHECK_MEMBER_STATUS(expirationTime: model.vipExpire) == .subscribe{
                content_label.setImageForAllStates(SC.YYMemberSignImage)
            }else if model.faceAuditState == 3{
                content_label.setImageForAllStates(SC.YYFaceSignImage)
            }else{
                content_label.setImageForAllStates(UIImage())
            }
            
            follow_btn.isSelected = model.isAttention

        }
    }
}

extension ST_MyFansCell{
    
    @objc fileprivate func push_user_center_handle(){
        
        guard let model = child_model else { return  }
        
            let vc = ST_PairsCenterViewController()
            
            vc.accid = model.accid

            let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
            
            nav.overCurrentContext = true
            
            GlobalPresent(toController: nav)
        
    }
    
    @objc fileprivate func follow_handle(){
        
        guard let model = child_model else { return  }
        
        let info = ST_MatchUserInfo.init(accid: model.accid, avatar: model.avatar, nickname: model.nickname)
        
        FollowBehavior(info: info, followControl: follow_btn) { (matched, err) in
            
            if let e = err{
                self.parentViewController?.toast(text: e)
            }else{
                
                model.isAttention = self.follow_btn.isSelected
            }
        }
        
    }
    
}


//MARK: - Model

class ST_MyFansCellModel : NSObject{

    var accid : String
    var avatar : String
    var createInt : Int
    var faceAuditState : Int
    var guardContent : String
    var guardTime : Int
    var id : Int
    var intimacy : Int
    var isAttention : Bool
    var nickname : String
    var vipExpire : Int
    var isSentProps:Bool
    var age:Int
    var address:String
    var hb_time:Int
    
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        
        accid = json["accid"].stringValue
        avatar = json["avatar"].stringValue
        createInt = json["create_int"].intValue
        faceAuditState = json["face_audit_state"].intValue
        guardContent = json["guard_content"].stringValue
        guardTime = json["guard_time"].intValue
        id = json["id"].intValue
        intimacy = json["intimacy"].intValue
        isAttention = json["is_attention"].boolValue
        nickname = json["nickname"].stringValue
        vipExpire = json["vip_expire"].intValue
        
        age = json["age"].intValue
        address = json["address"].stringValue
        hb_time = json["hb_time"].intValue
        
        isSentProps = json["is_send_props"].boolValue
        
        super.init()
    }
}
